package com.ruoyi.web.controller.campus;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyCommodityType;
import com.ruoyi.campus.service.IMyCommodityTypeService;

/**
 * 类别Controller
 *
 * @author zht
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/campus/commType")
public class MyCommodityTypeController extends BaseController {
    @Autowired
    private IMyCommodityTypeService myCommodityTypeService;

    /**
     * 查询类别列表
     */
    @PreAuthorize("@ss.hasAnyPermi('campus:commType:list,campus:commodity:list,campus:userCommodity:list')")
    @GetMapping("/list")
    public AjaxResult list(MyCommodityType myCommodityType) {
        List<MyCommodityType> types = myCommodityTypeService.selectMyCommodityTypeList(myCommodityType);
        return AjaxResult.success(types);
    }

    /**
     * 查询类别列表(排除节点)
     */
    @PreAuthorize("@ss.hasPermi('campus:commType:list')")
    @GetMapping("/list/exclude/{typeId}")
    public AjaxResult excludeChild(@PathVariable(value = "typeId", required = false) Long typeId) {
        List<MyCommodityType> types = myCommodityTypeService.selectMyCommodityTypeList(new MyCommodityType());
        Iterator<MyCommodityType> it = types.iterator();
        while (it.hasNext()) {
            MyCommodityType t = (MyCommodityType) it.next();
            if (t.getTypeId().intValue() == typeId
                    || ArrayUtils.contains(StringUtils.split(t.getAncestors(), ","), typeId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(types);
    }

    /**
     * 获取类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:commType:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId) {
        return AjaxResult.success(myCommodityTypeService.selectMyCommodityTypeByTypeId(typeId));
    }

    /**
     * 获取类别下拉树列表
     */
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(MyCommodityType myCommodityType){
        List<MyCommodityType> types = myCommodityTypeService.selectMyCommodityTypeList(myCommodityType);
        return AjaxResult.success(myCommodityTypeService.buildTypeTreeSelect(types));
    }

    /**
     * 新增类别
     */
    @PreAuthorize("@ss.hasPermi('campus:commType:add')")
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCommodityType myCommodityType) {
        if (UserConstants.NOT_UNIQUE.equals(myCommodityTypeService.checkTypeNameUnique(myCommodityType))){
            return AjaxResult.error("新增类别'" + myCommodityType.getTypeName() + "'失败，类别名称已存在");
        }
        myCommodityType.setCreateBy(getUsername());
        return toAjax(myCommodityTypeService.insertMyCommodityType(myCommodityType));
    }

    /**
     * 修改类别
     */
    @PreAuthorize("@ss.hasPermi('campus:commType:edit')")
    @Log(title = "类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCommodityType myCommodityType) {
        if (UserConstants.NOT_UNIQUE.equals(myCommodityTypeService.checkTypeNameUnique(myCommodityType))){
            return AjaxResult.error("修改类别'" + myCommodityType.getTypeName() + "'失败，类别名称已存在");
        }else if (myCommodityType.getParentId().equals(myCommodityType.getTypeId())){
            return AjaxResult.error("修改类别'" + myCommodityType.getTypeName() + "'失败，上级类别不能是自己");
        }
        myCommodityType.setUpdateBy(getUsername());
        return toAjax(myCommodityTypeService.updateMyCommodityType(myCommodityType));
    }

    /**
     * 删除类别
     */
    @PreAuthorize("@ss.hasPermi('campus:commType:remove')")
    @Log(title = "类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Long typeId) {
        if (myCommodityTypeService.hasChildByTypeId(typeId)){
            return AjaxResult.error("存在下级类别，不允许删除");
        }
        if (myCommodityTypeService.checkTypeExistComm(typeId)){
            return AjaxResult.error("该类别下有商品，不允许删除");
        }
        return toAjax(myCommodityTypeService.deleteMyCommodityTypeByTypeId(typeId));
    }
}
